// 代码自动生成，请勿更改
using System;
using System.Text;
using Se.Model.Host;
using System.Collections.Generic;
using Se.Protocol;

namespace Se.Model.Generator
{
    /// <summary>
    /// 对于接受到的数据包进行进一步的处理
    /// </summary>
	public class ServerReceivedHandler
	{
		private IServerReceived Handler;

        public ServerReceivedHandler(IServerReceived handler)
        {
            this.Handler = handler;
        }
		
		public void Handle(SocketSession session, ServerMessage package)
		{
			var g_messageType = package.Type;
            var g_field = package[ServerMessage.Descriptor.FindFieldByNumber((int)g_messageType)];
			switch(g_messageType)
			{

				case ServerMessage.Types.ServerType.ErrorInfo:
					var field_ErrorInfo = g_field as ErrorInfo;
					this.Handle_ErrorInfo(session, field_ErrorInfo);
					break;
				case ServerMessage.Types.ServerType.Login:
					var field_Login = g_field as Login;
					this.Handle_Login(session, field_Login);
					break;
				case ServerMessage.Types.ServerType.Register:
					var field_Register = g_field as Register;
					this.Handle_Register(session, field_Register);
					break;
				case ServerMessage.Types.ServerType.Chat:
					var field_Chat = g_field as Chat;
					this.Handle_Chat(session, field_Chat);
					break;
				case ServerMessage.Types.ServerType.CreatePlayer:
					var field_CreatePlayer = g_field as CreatePlayer;
					this.Handle_CreatePlayer(session, field_CreatePlayer);
					break;
			}
		}


		private void Handle_ErrorInfo(SocketSession session, ErrorInfo package)
		{
			this.Handler.PtRequest_ErrorInfo(session, package.ErrorType, package.Msg);
		}		

		private void Handle_Login(SocketSession session, Login package)
		{
			this.Handler.PtRequest_Login(session, package.UserName, package.Pwd);
		}		

		private void Handle_Register(SocketSession session, Register package)
		{
			this.Handler.PtRequest_Register(session, package.UserName, package.Pwd);
		}		

		private void Handle_Chat(SocketSession session, Chat package)
		{
			this.Handler.PtRequest_Chat(session, package.MsgType, package.Message);
		}		

		private void Handle_CreatePlayer(SocketSession session, CreatePlayer package)
		{
			this.Handler.PtRequest_CreatePlayer(session, package.Name, package.RoleTypeID);
		}		
	}

	public interface IServerReceived
	{
		void PtRequest_ErrorInfo(SocketSession session, Int32 errorType, String msg);
		void PtRequest_Login(SocketSession session, String userName, String pwd);
		void PtRequest_Register(SocketSession session, String userName, String pwd);
		void PtRequest_Chat(SocketSession session, Int32 msgType, String message);
		void PtRequest_CreatePlayer(SocketSession session, String name, Int32 roleTypeID);
	}



	public static class PS_Send
	{
		#region ErrorInfo

		public static void ErrorInfo(SocketSession session, Int32 errorType, String msg)
		{
			var g_data = ErrorInfoToByte(errorType,msg);
			session.SendToClient(g_data);
		}
		public static void BroadcastErrorInfo(List<SocketSession> sessions, Int32 errorType, String msg)
		{
			var g_data = ErrorInfoToByte(errorType,msg);
			sessions.ForEach(session => session.SendToClient(g_data));
		}
		private static byte[] ErrorInfoToByte(Int32 errorType, String msg)
		{
			ClientMessage.Builder g_commonMessage = new ClientMessage.Builder();
			ErrorInfo.Builder g_msg = new ErrorInfo.Builder();
			g_msg.SetErrorType(errorType).SetMsg(msg);
			g_commonMessage.SetType(ClientMessage.Types.ClientType.ErrorInfo);
			g_commonMessage.SetErrorInfo(g_msg);
            var g_data = g_commonMessage.Build().ToByteArray();
			return g_data;
		}
		#endregion

		#region LoginOK

		public static void LoginOK(SocketSession session, Int64 accountID, String userName, String message)
		{
			var g_data = LoginOKToByte(accountID,userName,message);
			session.SendToClient(g_data);
		}
		public static void BroadcastLoginOK(List<SocketSession> sessions, Int64 accountID, String userName, String message)
		{
			var g_data = LoginOKToByte(accountID,userName,message);
			sessions.ForEach(session => session.SendToClient(g_data));
		}
		private static byte[] LoginOKToByte(Int64 accountID, String userName, String message)
		{
			ClientMessage.Builder g_commonMessage = new ClientMessage.Builder();
			LoginOK.Builder g_msg = new LoginOK.Builder();
			g_msg.SetAccountID(accountID).SetUserName(userName).SetMessage(message);
			g_commonMessage.SetType(ClientMessage.Types.ClientType.LoginOK);
			g_commonMessage.SetLoginOK(g_msg);
            var g_data = g_commonMessage.Build().ToByteArray();
			return g_data;
		}
		#endregion

		#region RegisterOK

		public static void RegisterOK(SocketSession session, Int64 accountID, String userName)
		{
			var g_data = RegisterOKToByte(accountID,userName);
			session.SendToClient(g_data);
		}
		public static void BroadcastRegisterOK(List<SocketSession> sessions, Int64 accountID, String userName)
		{
			var g_data = RegisterOKToByte(accountID,userName);
			sessions.ForEach(session => session.SendToClient(g_data));
		}
		private static byte[] RegisterOKToByte(Int64 accountID, String userName)
		{
			ClientMessage.Builder g_commonMessage = new ClientMessage.Builder();
			RegisterOK.Builder g_msg = new RegisterOK.Builder();
			g_msg.SetAccountID(accountID).SetUserName(userName);
			g_commonMessage.SetType(ClientMessage.Types.ClientType.RegisterOK);
			g_commonMessage.SetRegisterOK(g_msg);
            var g_data = g_commonMessage.Build().ToByteArray();
			return g_data;
		}
		#endregion

		#region UpdateChatMsg

		public static void UpdateChatMsg(SocketSession session, ChatMsg[] content)
		{
			var g_data = UpdateChatMsgToByte(content);
			session.SendToClient(g_data);
		}
		public static void BroadcastUpdateChatMsg(List<SocketSession> sessions, ChatMsg[] content)
		{
			var g_data = UpdateChatMsgToByte(content);
			sessions.ForEach(session => session.SendToClient(g_data));
		}
		private static byte[] UpdateChatMsgToByte(ChatMsg[] content)
		{
			ClientMessage.Builder g_commonMessage = new ClientMessage.Builder();
			UpdateChatMsg.Builder g_msg = new UpdateChatMsg.Builder();
			g_msg.AddRangeContent(content);
			g_commonMessage.SetType(ClientMessage.Types.ClientType.UpdateChatMsg);
			g_commonMessage.SetUpdateChatMsg(g_msg);
            var g_data = g_commonMessage.Build().ToByteArray();
			return g_data;
		}
		#endregion

		#region CreatePlayerSuccess

		public static void CreatePlayerSuccess(SocketSession session, Int64 id, String name, Int32 roleTypeID)
		{
			var g_data = CreatePlayerSuccessToByte(id,name,roleTypeID);
			session.SendToClient(g_data);
		}
		public static void BroadcastCreatePlayerSuccess(List<SocketSession> sessions, Int64 id, String name, Int32 roleTypeID)
		{
			var g_data = CreatePlayerSuccessToByte(id,name,roleTypeID);
			sessions.ForEach(session => session.SendToClient(g_data));
		}
		private static byte[] CreatePlayerSuccessToByte(Int64 id, String name, Int32 roleTypeID)
		{
			ClientMessage.Builder g_commonMessage = new ClientMessage.Builder();
			CreatePlayerSuccess.Builder g_msg = new CreatePlayerSuccess.Builder();
			g_msg.SetId(id).SetName(name).SetRoleTypeID(roleTypeID);
			g_commonMessage.SetType(ClientMessage.Types.ClientType.CreatePlayerSuccess);
			g_commonMessage.SetCreatePlayerSuccess(g_msg);
            var g_data = g_commonMessage.Build().ToByteArray();
			return g_data;
		}
		#endregion

	}
}
